<?php

use yii\helpers\Html;
use yii\helpers\Url;
use yii\grid\GridView;

/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('debug', 'Menus');
$this->params['breadcrumbs'][] = $this->title;
$this->registerJs('
$(document).on("click", "[filter=\'delete-all\']", function(){
    var params = {};
    var cp = $("meta[name=\'csrf-param\']").attr("content");
    var ct = $("meta[name=\'csrf-token\']").attr("content");
    var ids = new Array();
    $("[name=\'ids\[\]\']:checked").each(function(){
        ids.push($(this).val());
    });

    params["ids"] = ids.join(",");
    params[cp] = ct;
    var result = confirm("Are you sure you want to delete the selected items?");
    if (!result) {
        e.preventDefault(); // 阻止默认行为，即不进行删除操作
    } else {
        // 可以添加额外的逻辑，例如禁用按钮等，以避免重复提交等。
        $.ajax({
            url:"'.Url::to(['delete']).'&id="+ids.join(","),
            type:"post",
            data:params,
            success:function(data){
                console.log(data);
            },
            error:function(err){
                console.log(err);
            }
        });
    }
});
', \yii\web\View::POS_END);
?>
<div class="menu-index">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <?= empty( $list['pid'] ) ? '' : Html::a(Yii::t('debug', 'Prev Level'), ['index','id'=>isset($list['pid']) ? $list['pid'] : 0], ['class' => 'btn btn-primary']) ?>
        <?= Html::a(Yii::t('debug', 'Create'), ['create', 'pid'=>isset($list['pid']) ? $list['pid'] : 0], ['class' => 'btn btn-success']) ?>
        <?= Html::a(Yii::t('debug', 'Delete All'), 'javascript:void(0);', ['class' => 'btn btn-warning', 'filter'=>'delete-all']) ?>
    </p>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'options'=>['id'=>'grid'],
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            ['class' => 'yii\grid\CheckboxColumn', 'name'=>'ids',  'checkboxOptions' => function ($model, $key, $index, $column) {
                return ['value' => $model['id']];
            }],
            // 'id',
            // 'name',
            // 'url:url',
            ['label'=>Yii::t('debug', 'Name'),
            'format'=>'html',
            'value'=>function($m){
                $url = !empty($m['url']) ? $m['url'] : ['/'.$m['module_id'] . '/'. $m['controller_id'] . '/' . $m['action_id'], 'type'=>!empty($m['type']) ? $m['type'] : null];
                return Html::a($m['name'], $url, ['target'=>'_blank']);
            }],
            // 'pid',
            // 'status',
            'i18n',
            // 'class',
            ['label'=>Yii::t('debug', 'Status'),
            'value'=>function($m){
                return $m['status']!=1 ? Yii::t('debug', 'Disable') : Yii::t('debug', 'Active');
            }],
            ['label'=>Yii::t('debug', 'Created At'),
            'value'=>function($m){
                return empty($m['created_at']) ? '' : date('Y-m-d H:i:s', $m['created_at']);
            }],
            ['label'=>Yii::t('debug', 'Updated At'),
            'value'=>function($m){
                return empty($m['updated_at']) ? '' : date('Y-m-d H:i:s', $m['updated_at']);
            }],

            ['class' => 'yii\grid\ActionColumn',
            'template' => '{index} {update} {delete}',
            'buttons' => [
                // 下面代码来自于 yii\grid\ActionColumn 简单修改了下
                'index' => function ($url, $model, $key) {
                  $options = [
                    'title' => Yii::t('yii', 'View'),
                    'aria-label' => Yii::t('yii', 'View'),
                    'data-pjax' => '0',
                  ];
                  return $model['pid'] != 0 ? '' : Html::a(\Yii::t('debug','Subordinate ID').'('.(isset($model['child']) ? $model['child'] : 0 ).')</span>', ['index', 'pid'=>$model['id']], $options);
                },
                'update' => function ($url, $model, $key) {
                  
                  return Html::a('<span class="glyphicon glyphicon-pencil"></span>', ['update', 'id'=>$model['id']]);
                },
                'delete' => function ($url, $model, $key) {
                  $options = [
                    'title' => Yii::t('yii', 'Delete'),
                    'aria-label' => Yii::t('yii', 'Delete'),
                    'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
                    'data-method' => 'post',
                    'data-pjax' => '0',
                  ];
                  return Html::a('<span class="glyphicon glyphicon-trash"></span>', ['delete', 'id'=>$model['id']], $options);
                },
              ]
            ],
        ],
    ]); ?>
</div>
